package com.yoda.mall.admin.controller;

import com.yoda.mall.admin.service.RoleService;
import com.yoda.mall.model.entity.ums.Menu;
import com.yoda.mall.model.entity.ums.Role;
import com.yoda.mall.model.vo.PageResult;
import com.yoda.mall.model.vo.R;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("role")
public class RoleController {
    @Autowired
    private RoleService roleService;

    /**
     * 给角色分配菜单
     * @param menuIds
     * @param roleId
     * @return
     */
    @PostMapping("allocMenu")
    public R allocMenu(@RequestParam int[]  menuIds, @RequestParam Integer roleId){
        roleService.allocMenu(menuIds,roleId);
        return R.success(null);
    }

    /**
     * 给角色分配资源
     * @param resourceIds
     * @param roleId
     * @return
     */
    @PostMapping("allocResource")
    public R allocResource(@RequestParam int[]  resourceIds, @RequestParam Integer roleId){
        roleService.allocResource(resourceIds,roleId);
        return R.success(null);
    }
    /**
     * 分页模糊查询角色
     * @param pageNum
     * @param pageSize
     * @param keyword
     * @return
     */
    @GetMapping("list")
    public R<PageResult<Role>> list( @RequestParam(value = "pageNum",required = false,defaultValue = "1") Integer pageNum,
                                     @RequestParam(value = "pageSize",required = false,defaultValue = "5")Integer pageSize, String keyword) {
        PageResult<Role> pageResult = roleService.list(keyword, pageNum, pageSize);
        return R.success(pageResult);
    }

    /**
     * 获取所有角色
     * @return
     */
    @GetMapping("listAll")
    public R<List<Role>> listAll(){
        List<Role> list = roleService.listAll();
        return R.success(list);
    }

    /**
     * 获取角色菜单
     * @param roleId
     * @return
     */
    @GetMapping("listMenu/{roleId}")
    public R<List<Menu>> listMenu(@PathVariable Integer roleId){
        List<Menu> list = roleService.listMenu(roleId);
        return R.success(list);
    }

    /**
     * 获取角色资源
     * @param roleId
     * @return
     */
    @GetMapping("listResource/{roleId}")
    public R<List<Menu>> listResource(@PathVariable Integer roleId){
        List<Menu> list = roleService.listResource(roleId);
        return R.success(list);
    }
    /**
     * 添加角色
     * @param role
     * @return
     */
    @PostMapping("create")
    public R create(@RequestBody Role role) {
        roleService.create(role);
        return R.success(null);
    }

    /**
     * 批量删除角色
     * @param ids
     * @return
     */
    @PostMapping("delete")
    public R delete(@RequestParam int[] ids) {
        roleService.delete(ids);
        return R.success(null);
    }

    /**
     * 修改角色
     * @param role
     * @return
     */
    @PostMapping("update/{id}")
    public R update(@PathVariable Integer id,@RequestBody Role role){
        roleService.update(id,role);
        return  R.success(null);
    }

    /**
     * 修改角色状态
     * @param id
     * @param status
     * @return
     */
    @PostMapping("updateStatus/{id}")
    public R updateStatus(@PathVariable Integer id,@RequestParam Integer  status){
        roleService.updateStatus(id,status);
        return  R.success(null);
    }

}
